package com.zl.codereview;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ui.Messages;
import com.zl.codereview.git.GitService;
import com.zl.codereview.review.ReviewContent;
import com.zl.codereview.util.IdeaUtils;
import com.zl.codereview.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CodeReviewRemoveAction extends AnAction {

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        if (e.getProject() != null && editor != null) {
            // 获取光标所在行号
            int lineNum = editor.getCaretModel().getLogicalPosition().line + 1; // 行号从1开始计数
            //代码评审路径
            String codeReviewPath = IdeaUtils.codeReviewPath(e);
            //代码评审内容
            List<ReviewContent> reviewContents  = IdeaUtils.parseObject(codeReviewPath);
            if(!ObjectUtils.isEmpty(reviewContents)){
                ReviewContent reviewContent = reviewContents.stream().filter(r -> r.getNumber() == lineNum)
                        .findFirst().orElse(null);
                if(reviewContent != null){
                    // 显示一个确认对话框
                    int answer = Messages.showYesNoDialog(e.getProject(),
                            reviewContent.getContent(),  // 提示信息
                            "确定移除这个Review吗?",              // 标题
                            Messages.getQuestionIcon());   // 图标

                    // 根据用户选择执行相应操作
                    if (answer == Messages.YES) {
                        reviewContents.removeIf(r->r.getNumber() == lineNum);
                        IdeaUtils.writeObject(reviewContents,codeReviewPath);
                    }
                }
            }

        }



    }


    @Override
    public void update(@NotNull AnActionEvent e) {
        //模块路径
        String modulePath = IdeaUtils.getModuleFilePath(e);
        //代码评审路径
        String codeReviewPath = IdeaUtils.codeReviewPath(e);
        //当前用户
        String user = GitService.getInstance().getUser(modulePath);
        //当前点击所在行号
        Editor editor = e.getData(PlatformDataKeys.EDITOR);
        if (e.getProject() != null && editor != null) {
            // 获取光标所在行号
            int lineNum = editor.getCaretModel().getLogicalPosition().line + 1; // 行号从1开始计数
            //代码评审内容
            List<ReviewContent> reviewContents  = IdeaUtils.parseObject(codeReviewPath);
            if(!ObjectUtils.isEmpty(reviewContents)) {
                ReviewContent reviewContent = reviewContents.stream().filter(r -> r.getNumber() == lineNum
                                && r.getAuthor() != null && r.getAuthor().equals(user))
                        .findFirst().orElse(null);
                if (reviewContent != null) {
                    e.getPresentation().setEnabled(true);
                    return;
                }
            }
        }
        e.getPresentation().setEnabled(false);
    }
}
